package standalone;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

public class Train {
	//Pierwszy integer oznacza nr klasy np 0 - Brak dostepu, 1 Dostep. Klasa oznacza obiekt dla danej klasy
	private HashMap<Integer,Klasa> klasy = new HashMap<Integer,Klasa>();
	
	
	public void readTrainData(){
		
		try {
			Scanner in = new Scanner(new File("resources/train.csv"));
			//usuwamy header
			in.nextLine();
			String[] values;
			
			int val;
			while(in.hasNextLine()){
				values = in.nextLine().split(",");
				val = Integer.valueOf(values[0]);
				//values[0] jest to kolumna ACTION
				Klasa kl = klasy.get(val);
				if(kl == null){
					kl = new Klasa(val);
					klasy.put(val, kl);
				}
				kl.learn(values);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	
	public void testData(){
		PrintWriter out = null;
		try {
			Scanner in = new Scanner(new File("resources/test.csv"));
			out = new PrintWriter("wynik_bayes.csv");
			//naglowowki cutoff
			in.nextLine();
			out.println("id,ACTION");
			
			int id = 1;
			int klas = 0;
			double max = 0;
			double pred = 0;
			Klasa klasa;
			Integer[] values;
			String line;
			
			while(in.hasNextLine()){
				line = in.nextLine();
				values = Row.stringToNumeric(line.split(","));
				
				for(Integer kl: klasy.keySet()){
					klasa = klasy.get(kl);
					pred = klasa.predict(values);
					//System.out.println("Dla klasy " + kl + " predukcja wynosik " + pred);
					if(pred > max){
						//System.out.println("Nowy pred " + pred + " jest wiekszy niz stary " + max);
						max = pred;
						klas = kl;
					}
				}
				out.println(id + "," + Ranked.rank(klas, max));
				max = 0;
				klas = 0;
				id++;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.flush();
		
	}
	

}
